package com.manager.action.file;

import java.util.List;
import java.util.Random;

import org.common.util.DateHelper;
import org.common.util.TranslateHelper;
import org.upload.FormFile;
import org.wface.web.core.ActionSupport;

import com.manager.service.file.FileService;
import com.manager.service.news.CategoryService;
import com.util.FileUtil;
import com.web.model.Category;
import com.web.model.File;
import com.web.model.vo.query.FileQuery;
import com.web.tag.bean.PageModel;

public class FileAction extends ActionSupport {

	private String page;

	private String fid;
	private String cid;
	private String title;
	private String info;
	private FormFile file;

	public String list() {
		PageModel model = new PageModel();
		int pageNum = TranslateHelper.parseInteger(page);
		model.setPageNum(pageNum);

		FileService fileService = new FileService();
		FileQuery query = new FileQuery();
		query.setCid(TranslateHelper.parseInteger(cid));
		query.setTitle(title);
		List<File> fileList = fileService.findList(model, query);
		getRequest().setAttribute("fileList", fileList);
		getRequest().setAttribute("query", query);
		CategoryService cateService = new CategoryService();
		List<Category> categorys = cateService.findList();
		getRequest().setAttribute("categorys", categorys);

		model.setPaths(getBasePath() + "/manager/file/list.do?");
		getRequest().setAttribute(PageModel.PAGEMODEL, model);
		return "list";
	}

	public String create() {
		CategoryService cateService = new CategoryService();
		List<Category> categorys = cateService.findList();
		getRequest().setAttribute("categorys", categorys);
		return "create";
	}

	public String save() {
		File upfile = new File();
		upfile.setCid(TranslateHelper.parseInteger(cid));
		upfile.setFid(TranslateHelper.parseInteger(fid));
		upfile.setInfo(info);
		upfile.setTitle(title);

		String filePath = "upload/files/" + DateHelper.getDateStr("yyyyMMdd") + "/";
		String fileurl = FileUtil.creatFile(getRequest().getSession().getServletContext().getRealPath("/") + filePath);
		String fileName = "file" + System.currentTimeMillis() + "_" + new Random().nextInt(9999) + "." + file.getFileExt();
		fileurl += fileName;
		try {
			file.saveAs("/" + fileurl, FormFile.SAVEAS_PHYSICAL);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// upfile.setFilepath(fileurl);
		upfile.setFilepath(filePath + fileName);
		FileService fileService = new FileService();
		fileService.saveFile(upfile);
		return "!/manager/file/list.do";
	}

	public String delete() {
		FileService fileService = new FileService();
		fileService.deleteFile(TranslateHelper.parseInteger(fid));
		return "!/manager/file/list.do";
	}

	private String getBasePath() {
		String basePath = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + getRequest().getContextPath();
		return basePath;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public void setFile(FormFile file) {
		this.file = file;
	}

	public void setFid(String fid) {
		this.fid = fid;
	}

}
